﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
namespace AnyOffice.general.system.attend
{
    public partial class duty_edit : ModulePage 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int dutyTypeId = (Request.QueryString["duty_type_id"] != null) ? Convert.ToInt32(Request.QueryString["duty_type_id"].ToString()) : 0;
                this.ViewState["DutyTypeId"] = dutyTypeId;
                DataTable table = new AttendConfigDao().SelectByDutyTypeId(base.Conn, dutyTypeId);
                if (table.Rows.Count > 0)
                {
                    this.txtDutyName.Value = table.Rows[0]["DutyName"].ToString();
                    if (table.Rows[0]["DutyTime1"] != DBNull.Value)
                    {
                        this.txtDutyTime1.Value = table.Rows[0]["DutyTime1"].ToString();
                    }
                    if (table.Rows[0]["DutyTime2"] != DBNull.Value)
                    {
                        this.txtDutyTime2.Value = table.Rows[0]["DutyTime2"].ToString();
                    }
                    if (table.Rows[0]["DutyTime3"] != DBNull.Value)
                    {
                        this.txtDutyTime3.Value = table.Rows[0]["DutyTime3"].ToString();
                    }
                    if (table.Rows[0]["DutyTime4"] != DBNull.Value)
                    {
                        this.txtDutyTime4.Value = table.Rows[0]["DutyTime4"].ToString();
                    }
                    if (table.Rows[0]["DutyTime5"] != DBNull.Value)
                    {
                        this.txtDutyTime5.Value = table.Rows[0]["DutyTime5"].ToString();
                    }
                    if (table.Rows[0]["DutyTime6"] != DBNull.Value)
                    {
                        this.txtDutyTime6.Value = table.Rows[0]["DutyTime6"].ToString();
                    }
                    if (this.sltDutyType1.Items.FindByValue(table.Rows[0]["DutyType1"].ToString()) != null)
                    {
                        this.sltDutyType1.Items.FindByValue(table.Rows[0]["DutyType1"].ToString()).Selected = true;
                    }
                    if (this.sltDutyType2.Items.FindByValue(table.Rows[0]["DutyType2"].ToString()) != null)
                    {
                        this.sltDutyType2.Items.FindByValue(table.Rows[0]["DutyType2"].ToString()).Selected = true;
                    }
                    if (this.sltDutyType3.Items.FindByValue(table.Rows[0]["DutyType3"].ToString()) != null)
                    {
                        this.sltDutyType3.Items.FindByValue(table.Rows[0]["DutyType3"].ToString()).Selected = true;
                    }
                    if (this.sltDutyType4.Items.FindByValue(table.Rows[0]["DutyType4"].ToString()) != null)
                    {
                        this.sltDutyType4.Items.FindByValue(table.Rows[0]["DutyType4"].ToString()).Selected = true;
                    }
                    if (this.sltDutyType5.Items.FindByValue(table.Rows[0]["DutyType5"].ToString()) != null)
                    {
                        this.sltDutyType5.Items.FindByValue(table.Rows[0]["DutyType5"].ToString()).Selected = true;
                    }
                    if (this.sltDutyType6.Items.FindByValue(table.Rows[0]["DutyType6"].ToString()) != null)
                    {
                        this.sltDutyType6.Items.FindByValue(table.Rows[0]["DutyType6"].ToString()).Selected = true;
                    }
                    if (table.Rows[0]["General"] != DBNull.Value)
                    {
                        string str = "," + table.Rows[0]["General"].ToString() + ",";
                        foreach (ListItem item in this.cblGeneral.Items)
                        {
                            if (str.IndexOf("," + item.Value + ",") >= 0)
                            {
                                item.Selected = true;
                            }
                        }
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                AttendConfigDao dao2 = new AttendConfigDao();
                AttendConfigData data = new AttendConfigData();
                data.DutyTypeId = Convert.ToInt32(this.ViewState["DutyTypeId"].ToString());
                data.DutyName = this.txtDutyName.Value;
                if (this.txtDutyTime1.Value != string.Empty)
                {
                    data.DutyTime1 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime1.Value).ToString("HH:mm:ss");
                }
                if (this.txtDutyTime2.Value != string.Empty)
                {
                    data.DutyTime2 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime2.Value).ToString("HH:mm:ss");
                }
                if (this.txtDutyTime3.Value != string.Empty)
                {
                    data.DutyTime3 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime3.Value).ToString("HH:mm:ss");
                }
                if (this.txtDutyTime4.Value != string.Empty)
                {
                    data.DutyTime4 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime4.Value).ToString("HH:mm:ss");
                }
                if (this.txtDutyTime5.Value != string.Empty)
                {
                    data.DutyTime5 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime5.Value).ToString("HH:mm:ss");
                }
                if (this.txtDutyTime6.Value != string.Empty)
                {
                    data.DutyTime6 = Convert.ToDateTime(DateTime.Now.ToString("d") + " " + this.txtDutyTime6.Value).ToString("HH:mm:ss");
                }
                data.DutyType1 = Convert.ToInt32(this.sltDutyType1.Value);
                data.DutyType2 = Convert.ToInt32(this.sltDutyType2.Value);
                data.DutyType3 = Convert.ToInt32(this.sltDutyType3.Value);
                data.DutyType4 = Convert.ToInt32(this.sltDutyType4.Value);
                data.DutyType5 = Convert.ToInt32(this.sltDutyType5.Value);
                data.DutyType6 = Convert.ToInt32(this.sltDutyType6.Value);
                string str2 = string.Empty;
                foreach (ListItem item2 in this.cblGeneral.Items)
                {
                    if (item2.Selected)
                    {
                        str2 = str2 + item2.Value + ",";
                    }
                }
                if (str2 != string.Empty)
                {
                    data.General = str2;
                }
                dao2.Update(base.Conn, data);
                base.CloseConn();
                Response.Redirect("duty.aspx");
            }
        } 

    }
}
